//
//  menuView.swift
//  许鲜
//
//  Created by hbl on 16/4/23.
//  Copyright © 2016年 hbl. All rights reserved.
//

import UIKit
import AFNetworking
import MJExtension
import YYWebImage

protocol MenuViewDelegate {
    func menuViewOnBottomClick(topView:MenuView, isOpen:Bool);
}

class MenuView: UICollectionViewCell {
    var delegate:MenuViewDelegate?
    var isOpen = false
    lazy var btnArray = Array<AnyObject>()
    lazy var topView:UIView = {
        let topView = UIView()
       // topView.frame = CGRectMake(0, 0, mainW, 100)
        topView.clipsToBounds = true
        self.contentView.addSubview(topView)
        return topView
    }()
    
    lazy var  bottomBtn:UIButton = {
        let bottomBtn = UIButton()
        self.contentView.addSubview(bottomBtn)
        return bottomBtn
    }()
    lazy var centDataArray:Array<AnyObject> = Array()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        getData()
        for _ in 0..<8 {
            let btn = VerticalButton()
            topView.addSubview(btn)
            btnArray.append(btn)
            
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let btnW:CGFloat = 60
        let btnH:CGFloat = btnW + CGFloat(15)
        let border:CGFloat = 15
        let margin:CGFloat = 20
        let lineMargin = (mainW - (btnW * 4 + border * 2))/3
        for i in 0..<btnArray.count {
            let btn = btnArray[i] as! VerticalButton
            
            let row = i/4
            let btnY = border + CGFloat(row) * (btnH + margin)
            let btnX = border + CGFloat(i%4) * (btnW + lineMargin)
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH)
        }
        
            topView.frame = CGRectMake(0, 0, mainW, contentView.frame.size.height - 25)
        
            self.bottomBtn.frame = CGRectMake(0, CGRectGetMaxY(self.topView.frame), mainW, 25)
            self.bottomBtn.setImage(UIImage(named: "VIPqiehuan"), forState: UIControlState.Normal)
            bottomBtn.backgroundColor = UIColor.clearColor()
            self.bottomBtn.imageView?.contentMode = UIViewContentMode.Center
            self.bottomBtn.addTarget(self, action: #selector(MenuView.onBottomClick), forControlEvents: UIControlEvents.TouchUpInside)
            //self.contentView.addSubview(self.bottomBtn)
        
            //self.frame.size.height = CGRectGetMaxY(self.bottomBtn.frame)
        
        
    }
    
    func onBottomClick(){
        
       
        //isOpen = !isOpen
        if !isOpen {
            bottomBtn.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
        }else{
            bottomBtn.imageView?.transform = CGAffineTransformIdentity
        }
         delegate?.menuViewOnBottomClick(self,isOpen: isOpen)
    }
    
    func getData(){
        let manager = AFHTTPSessionManager()
        manager.responseSerializer = AFHTTPResponseSerializer()
        /* cityId	420100
         storeId	285
         token	9984469351296d6352fe44f2ff4ba277*/
        var params = Dictionary<String,String>()
        params["cityId"] = "420100"
        params["storeId"] = "285"
        params["token"] = "9984469351296d6352fe44f2ff4ba277"
        manager.POST("http://mobile.xuxian.com/index/getList/?ver=2016040101", parameters: params, progress: nil, success: { (_, JSON) in
            do{
                
               let str = try NSJSONSerialization.JSONObjectWithData(JSON as! NSData, options: NSJSONReadingOptions.MutableContainers)
               let temp = str["data"] as! Dictionary<String,AnyObject>
               let tempArray = temp["app"] as! Array<AnyObject>
               self.centDataArray = CenterModel.mj_objectArrayWithKeyValuesArray(tempArray) as Array
                self.setData()
                //print(str)
            }catch{
            
            }
            
            
            }) { (_, error) in
                print(error)
        }
    }
    
    func setData(){
        for i in 0..<centDataArray.count {
            let btn = btnArray[i] as! VerticalButton
            let data = centDataArray[i] as! CenterModel
            btn.yy_setImageWithURL(NSURL(string: data.img!), forState: UIControlState.Normal, placeholder: nil)
            btn.setTitle(data.title, forState: UIControlState.Normal)
            btn.model = data
            btn.addTarget(self, action: #selector(onClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
        }
    }
    
    func onClick(btn:VerticalButton){
        let str = (btn.model?.message)! as NSString
        if (str.length != 0) {
            let vc = CenterDetailViewController()
            vc.message = btn.model?.message
            let nav = BLNavigationViewController(rootViewController: vc)
            UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
            
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
